package com.ytsky.mybatis.demo;

import com.ytsky.mybatis.demo.entity.Customer;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 *使用MyBatis访问数据库
 */
public class MyBatisSimpleDemo {

    public static void main(String[] args) throws IOException {
        //1.读取配置文件
        String configFile="mybatis-config.xml";
       final InputStream inputStream = Resources.getResourceAsStream(configFile);
       //2.创建SqlSessionFactory对象,启动MyBatis运行环境
       final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//       findAllCustomer(sqlSessionFactory);
      findCustomerById(sqlSessionFactory,1);

        //创建一个新的Customer对象，封装客户数据,增加数据
//        Customer customer=new Customer();
//        customer.setName("customer002");
//        customer.setTel("99998888");
//        customer.setAddress("江苏南京");
//        customer.setRelName("王五");
//        customer.setOther("大数据平台技术");
//        insertCustomer(sqlSessionFactory,customer);

      //创建一个新的Customer对象，封装要修改的属性数据及条件
//        Customer customer=new Customer();
//        customer.setId(75);
//        customer.setRelName("李四");
//        System.out.println(updateByIdCustomerRelName(sqlSessionFactory,customer));
      //删除
//        System.out.println(deleteById(sqlSessionFactory,73));
    }

    public static void findAllCustomer(SqlSessionFactory factory){
        //3.打开到数据库的会话，初始化到数据库的连接，准备执行数据库操作
        final SqlSession sqlSession = factory.openSession();

        //4.执行数据库
        final List<Customer> customers = sqlSession.selectList("CustomerMapper.selectAll");
        //5.关闭资源
        sqlSession.close();
        //6.使用已获得的数据
        System.out.println(customers);
    }
    //根据传入的id来查询某个客户的数据
    public static void findCustomerById(SqlSessionFactory factory,int id){
       final SqlSession sqlSession = factory.openSession();
       final   Customer customer=   sqlSession.selectOne("CustomerMapper.selectById",id);
       sqlSession.close();
       System.out.println(customer);
    }

    /**
     * 插入新的客户端
     * @param factory
     * @param customer
     * @return 返回的是实际插入的行数，如果为0
     */
    public static int insertCustomer(SqlSessionFactory factory,Customer customer){
      final   SqlSession session = factory.openSession(true);
      final   int i = session.insert("CustomerMapper.insert", customer);
     //手动提交数据库的更改，让数据库真正使数据持久化
//      session.commit();
      session.close();
      return i;
    }

    /**
     * 根据id修改客户的联系人数据
     * @param factory
     * @param customer
     * @return
     */
    public static int updateByIdCustomerRelName(SqlSessionFactory factory,Customer customer){
     final    SqlSession session = factory.openSession(true);
     final    int i = session.update("CustomerMapper.updateRelNameById", customer);
     session.close();
     return i;
    }

    public static int deleteById(SqlSessionFactory sqlSessionFactory,int id){
       final SqlSession session = sqlSessionFactory.openSession(true);
       final int i = session.delete("CustomerMapper.deleteById", id);
       session.close();
       return i;
    }
}
